package com.cgbl.cg.adapter

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.widget.AppCompatImageView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.SpanUtils
import com.cgbl.cg.R
import com.robot.base.configs.AppConstants
import com.robot.base.model.PreferentPageBean
import com.robot.base.util.arouter.ARouterUtil
import com.robot.base.utils.pic.ImageLoaderUtils
import com.youth.banner.adapter.BannerAdapter

class DiscountAdapter(mDatas: List<PreferentPageBean>) : BannerAdapter<PreferentPageBean, DiscountAdapter.BannerViewHolder>(mDatas) {
    //创建ViewHolder，可以用viewType这个字段来区分不同的ViewHolder
    override fun onCreateHolder(parent: ViewGroup, viewType: Int): BannerViewHolder {
        //注意布局文件，item布局文件要设置为match_parent，这个是viewpager2强制要求的
        //或者调用BannerUtils.getView(parent,R.layout.banner_image_title_num);
        val view: View = LayoutInflater.from(parent.context)
            .inflate(R.layout.app_item_discount, parent, false)
        return BannerViewHolder(view)
    }

    //绑定数据
    override fun onBindView(holder: BannerViewHolder, data: PreferentPageBean, position: Int, size: Int) {
//        ImageLoaderUtils.getGlideManager()
//            .load(data)
////            .apply(ImageLoaderUtils.getRoundCornerGlideOption(10))
//            .into(holder.imageView)
        holder.clLayout.setOnClickListener {
            if(data.projectType == "0"){
                ARouterUtil.goToActivityWithBundle(AppConstants.Router.Mine.A_MINE_COURSE_PROFILE,
                    Bundle().apply {
                        putString(AppConstants.BundleKey.ID, data.projectId)
                    })
            }else {
                ARouterUtil.goToActivityWithBundle(AppConstants.Router.Mine.A_MINE_RESOURCE_PROFILE,
                    Bundle().apply {
                        putString(AppConstants.BundleKey.ID, data.projectId)
                    })
            }
        }

        holder.tvName.text = data.projectName
        holder.tvPrompt.text = data.projectKey
        SpanUtils.with(holder.tvPrice).append("￥${data.preferentPrice?:"0.00"}")
            .setForegroundColor(ColorUtils.getColor(R.color.app_color_cc2d2d)).create()

        ImageLoaderUtils.displayRound(holder.imageView.context, holder.imageView, data.projectCover, 10)
        //可以在布局文件中自己实现指示器，亦可以使用banner提供的方法自定义指示器，目前样式较少，后面补充
    }

    inner class BannerViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var imageView: ImageView
        var tvPrice: TextView
        var tvName: TextView
        var tvPrompt: TextView
        var clLayout: ConstraintLayout

        init {
            imageView = view.findViewById(R.id.item_iv_1)
            tvPrice = view.findViewById(R.id.item_tv_price)
            tvName = view.findViewById(R.id.item_tv_name)
            tvPrompt = view.findViewById(R.id.item_tv_prompt)
            clLayout = view.findViewById(R.id.item_cl_layout)
        }
    }
}